package site.xuxingcan.autologin;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by Administrator on 2016/9/29.
 */
public class FloatService extends Service {
    private FloatUtil floatUtil;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if ("toggleHome".equalsIgnoreCase(intent.getAction())) {
            floatUtil.toggleHome();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Intent notificationIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification noti = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            noti = new Notification.Builder(this)
                    .setContentTitle("登录程序")
                    .setContentText("防止杀死")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .build();
        }
        startForeground(12340, noti);
        floatUtil = new FloatUtil();
        floatUtil.createFloatView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (floatUtil != null) {
            floatUtil.dismiss();
        }
    }
}
